package jaxb;

import com.sun.xml.internal.txw2.annotation.XmlElement;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.*;
import java.io.File;
import java.io.Serializable;

/**
 * Author: hujian
 * Date: 2018/12/28 14:52
 * Description: <描述>
 */

//javaBean 代码

@XmlType(propOrder = {})
@XmlRootElement(name = "user")
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
public class UserBean implements Serializable {

    private String userName;
    private int age;
    private String role;
    private String bibi;

    public UserBean() {
    }

    public UserBean(String userName, int age, String role, String bibi) {
        this.userName = userName;
        this.age = age;
        this.role = role;
        this.bibi = bibi;
    }

    @XmlElement
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @XmlAttribute
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @XmlElement
    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    @XmlElement
    public String getBibi() {
        return bibi;
    }

    public void setBibi(String bibi) {
        this.bibi = bibi;
    }

    @Override
    public String toString() {
        return "UserBean{" +
                "userName='" + userName + '\'' +
                ", age=" + age +
                ", role='" + role + '\'' +
                ", bibi='" + bibi + '\'' +
                '}';
    }
}

class Test {
    public static void main(String[] args) {
        saveXmlTest();
        //getUserTest();
    }

    private static void saveXmlTest() {
        UserBean user = new UserBean("陈本布衣", 2018, "超级管理员", "瞎哔哔");
        File file = new File("E://user.xml");
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(UserBean.class);
            Marshaller marshaller = jaxbContext.createMarshaller();
            //格式化输出，即按标签自动换行，否则就是一行输出
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            //设置编码（默认编码就是utf-8）
            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            //是否省略xml头信息，默认不省略(false)
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);
            marshaller.marshal(user, file);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

    public static void getUserTest() {
        File file = new File("E://user.xml");
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(UserBean.class);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            UserBean user = (UserBean) unmarshaller.unmarshal(file);
            System.out.println(user.toString());
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}

